package com.ch4;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author woldier
 * @version 1.0
 * @description ReentrantLock
 * @date 2023/5/10 10:56
 **/
@Slf4j(topic = "c.Thread")
public class Demo11 {

    private static boolean cigarette = false;
    private static boolean takeout = false;

    private static ReentrantLock room = new ReentrantLock();

    private static Condition waitCigarette = room.newCondition();
    private static Condition waitTakeout = room.newCondition();


    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
            room.tryLock();

            try {
                while (!cigarette) {
                    log.debug("没烟,干不了活");
                    try {
                        waitCigarette.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                log.debug("有烟了开始干活");
            } finally {
                room.unlock();
            }

        }, "小男");

        Thread t2 = new Thread(() -> {
            room.tryLock();

            try {
                while (!cigarette) {
                    log.debug("没饭,干不了活");
                    try {
                        waitTakeout.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                log.debug("有饭了开始干活");
            } finally {
                room.unlock();
            }

        }, "小女");

        t1.start();
        TimeUnit.SECONDS.sleep(1);

        t2.start();
        TimeUnit.SECONDS.sleep(1);


        new Thread(() -> {
            while (true){
                try {
                    if (room.tryLock(1,TimeUnit.SECONDS)) break;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.debug("获取锁失败");
            }
            try {
                log.debug("送烟了");
                cigarette = true;
                waitCigarette.signal();

                TimeUnit.SECONDS.sleep(1);

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                room.unlock();
            }

        }, "送烟人").start();

//        TimeUnit.SECONDS.sleep(1);

        new Thread(() -> {

            while (true){
                try {
                    if (room.tryLock(1,TimeUnit.SECONDS)) break;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.debug("获取锁失败");
            }
            try {
                log.debug("送饭了");
                takeout = true;
                waitTakeout.signal();

                TimeUnit.SECONDS.sleep(1);

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                room.unlock();
            }
        }, "送饭人").start();


    }

}
